const app = getApp()

Page({
  data: {
    cacheSize: 0
  },

  onLoad() {
    this.calculateCacheSize()
  },

  onBack() {
    wx.navigateBack()
  },

  calculateCacheSize() {
    const res = wx.getStorageInfoSync()
    this.setData({
      cacheSize: (res.currentSize / 1024).toFixed(2)
    })
  },

  handleClearCache() {
    wx.showLoading({title: '清理中'})
    
    // 获取所有存储的key
    const {keys} = wx.getStorageInfoSync()
    
    // 保留这些关键数据
    const preserveKeys = ['token']
    
    // 只清除非关键数据
    keys.forEach(key => {
      if (!preserveKeys.includes(key)) {
        wx.removeStorageSync(key)
      }
    })
    
    this.calculateCacheSize()
    wx.hideLoading()
    wx.showToast({title: '清理完成'})
  },

  toggleNotify(e) {
    this.setData({notifyEnabled: e.detail.value})
    wx.showToast({
      title: e.detail.value ? '已开启通知' : '已关闭通知',
      icon: 'none'
    })
  },

  onFeedbackTap() {
    wx.navigateTo({url: '/packageA/feedback/index'})
  }
})